
import {http} from './config.js'


class Request{
	post(url,params={}){
		return new Promise((resolve,reject)=>{
			try{
				console.log(uni.getStorageSync('token'))
				uni.request({
					url:'/api/' + url,
					header:{
						"wechat-api-token": uni.getStorageSync('token') ? uni.getStorageSync('token'):''
					},
					method:"POST",
					data:params,
					success:res=>{
						uni.hideLoading()
						if(res.data.code == 0){
							resolve(res.data.data)
						}else{
							uni.showToast({
								title:res.data.msg,
								icon:"none"
							})
							reject(res.data)
						}
					}
				})
			}catch(e){
				//TODO handle the exception
				console.log(url+'接口报错，请检查：'+e)
			}
			
		})
	} 
	get(url,params={}){
		return new Promise((resolve,reject)=>{
			try{
				uni.request({
					url:'/api/' + url,
					header:{
						"wechat-api-token": uni.getStorageSync('token') ? uni.getStorageSync('token'):''
					},
					method:"GET",
					data:params,
					success:res=>{
						uni.hideLoading()
						if(res.data.code == 0){
							resolve(res.data.data)
						}else{
							uni.showToast({
								title:res.data.msg,
								icon:"none"
							})
							console.log(res.data.code)
							reject(res.data.code)
						}
					}
				})
			}catch(e){
				//TODO handle the exception
				console.log(url+'接口报错，请检查：'+e)
			}
			
		})
	} 
	upload(){
		return new Promise((resolve,reject)=>{
			try{
				uni.chooseImage({
					count: 1,
				    success: async (chooseImageRes) => {
				        const tempFilePaths = chooseImageRes.tempFilePaths;
						uni.showLoading({
							title:'上传中'
						})
				        let url = await this.uploadImg(tempFilePaths[0])
						uni.hideLoading()
						resolve(url)
				    }
				});
			}catch(e){
				//TODO handle the exception
				console.log('上传图片接口报错，请检查：'+e)
			}
			
		})
	}
	uploadImg(filePath){
		return new Promise((resolve,reject)=>{
			uni.uploadFile({
			    url: '/api/file/uploadOne', //仅为示例，非真实的接口地址
			    filePath: filePath,
				header:{
					"wechat-api-token": uni.getStorageSync('token') ? uni.getStorageSync('token'):''
				},
			    name: 'file',
			    success: (uploadFileRes) => {
					console.log(uploadFileRes)
					if(uploadFileRes.statusCode == 200){
						console.log(JSON.parse(uploadFileRes.data).data)
						resolve(JSON.parse(uploadFileRes.data).data.url)
					}else{
						uni.hideLoading()
						uni.showToast({
							title:'上传失败',
							icon:"none"
						})
					}
			    }
			});
		})
	}
	 uploadMany(num){
		return new Promise((resolve,reject)=>{
			try{
				uni.chooseImage({
					count: num,
				    success: async (chooseImageRes) => {
				        const tempFilePaths = chooseImageRes.tempFilePaths;
						uni.showLoading({
							title:'上传中'
						})
						let imgs=[]
						for(let i = 0;i<tempFilePaths.length;i++){
							let url = await this.uploadImg(tempFilePaths[i])
							console.log(url)
							imgs.push(url)
							
							if(imgs.length == tempFilePaths.length){
								uni.hideLoading()
								 resolve(imgs)
							}
						}
				        
				    }
				});
			}catch(e){
				//TODO handle the exception
				console.log('上传图片接口报错，请检查：'+e)
			}
			
		})
	}
}

export default new Request();
